/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import javax.persistence.*;
import jpapractica.*;

/**
 *
 * @author master
 */
public class DirectorManager {
    
    EntityManagerFactory emf;
    EntityManager managerDirector;
    
    public DirectorManager(){
        emf= Persistence.createEntityManagerFactory("GRLUPU");
        managerDirector= emf.createEntityManager();
    }
    
    public Director create (int id, String name, String lastname, String picture,
                            String birthplace, int dia, int mes, int ano){
        
        Director director = new Director(id, name, lastname, picture, birthplace,dia,mes,ano);
        
      
        managerDirector.getTransaction().begin();
        managerDirector.persist(director);
        managerDirector.getTransaction().commit();
        
        return director;
        
    }
    public List<Director> buscar(String palabra){
   
        Query busqueda =managerDirector.createQuery("SELECT p FROM Person p WHERE p.name='"+palabra+"'");
        List<Director> aux =busqueda.getResultList();
         return aux;
    }
    
    public void update(int id, String name, String lastname, String picture,
                            String birthplace, int dia, int mes, int ano){
        Director director = managerDirector.find(Director.class, id);
        managerDirector.getTransaction().begin();
        director.setName(name);
        director.setLastname(lastname);
        director.setBirthplace(birthplace);
        director.setPicture(picture);
        director.setBirthdate(dia,mes,ano);
        managerDirector.getTransaction().commit();        
    
    }
    public void delete (int id){
        Director director = managerDirector.find(Director.class, id);
        
        managerDirector.getTransaction().begin();
        managerDirector.remove(director);
        managerDirector.getTransaction().commit();
    }    
        
    
}
